package com.xh.jf_blog.controller.admin;

import com.xh.jf_blog.bean.Userinfo;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.common.util.SessionInfo;
import com.xh.jf_blog.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author PangJunjie
 * @Date 2024/2/1/001
 */
@RequestMapping("/admin/userinfo")
@Controller
public class UserinfoController extends SessionInfo {
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("/list")
    public String list(int role, Model model) {
        model.addAttribute("role", role);
        return "admin/userinfo/userinfo-list";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Userinfo userinfo) {
        if (getAdminId() != 1) {
            userinfo.setUserinfoId(getAdminId());
        } else {
            userinfo.setUserinfoId(null);
        }
        return userinfoService.getPage(page, limit, userinfo);
    }

    @GetMapping("/edit")
    public String edit(Integer userinfoId, String type, Model model) {
        if (userinfoId != null) {
            model.addAttribute("type", type);
            model.addAttribute("userinfo", userinfoService.getById(userinfoId));
        }
        return "admin/userinfo/userinfo-update";
    }

    @PostMapping(value = {"/update", "/status", "/password"})
    @ResponseBody
    public R update(Userinfo userinfo) {
        userinfoService.saveOrUpdate(userinfo);
        return R.ok();
    }

    @DeleteMapping("/delete/{userinfoId}")
    @ResponseBody
    public R delete(@PathVariable int userinfoId) {
        userinfoService.deleteById(userinfoId);
        return R.ok();
    }

    @GetMapping("/password")
    public String password() {
        return "admin/userinfo/userinfo-password";
    }

    @GetMapping("/check/password")
    @ResponseBody
    public R checkPassword(Userinfo userinfo) {
        return userinfoService.checkPassword(userinfo);
    }

}
